from datetime import datetime
from myApp.model.db import db


class Message(db.Model):
    __tablename__ = 'message'

    id = db.Column(db.String(100), primary_key=True)
    user_id = db.Column(db.String(100))
    text = db.Column(db.Text)
    room_id = db.Column(db.String(100), db.ForeignKey('room.id'), comment='对话ID')
    role = db.Column(db.String(255))
    model = db.Column(db.String(255))
    status = db.Column(db.String(100))
    create_time = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    del_flag = db.Column(db.Integer, default=1, nullable=False)

    def serialize(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'text': self.text,
            'room_id': self.room_id,
            'role': self.role,
            'model': self.model,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'dateTime': self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            'status': self.status
        }

    @staticmethod
    def create_message(id, user_id, text, room_id, role, model):
        existing_message = Message.query.get(id)
        if existing_message:
            # 如果存在重复的 ID，则执行更新操作
            Message.update_message(id, text, model)
        else:
            # 如果不存在重复的 ID，则执行插入操作
            message = Message(id=id, user_id=user_id, text=text, room_id=room_id, role=role, model=model)
            db.session.add(message)
            db.session.commit()

    @staticmethod
    def update_message(id, text, model):
        message = Message.query.filter_by(id=id).first()
        message.text = text
        message.model = model
        db.session.commit()

    @staticmethod
    def get_messages_by_user_and_room(user_id=None, room_id=None):
        messages = Message.query.filter_by(user_id=user_id, room_id=room_id, del_flag=1).order_by(
            Message.create_time.asc()).all()
        return messages

    @staticmethod
    def get_messages_by_room(room_id):
        messages = Message.query.filter_by(room_id=room_id, del_flag=1).order_by(
            Message.create_time.asc()).all()
        return messages

    @staticmethod
    def delete_message(message):
        message.del_flag = 0
        db.session.commit()

    # 根据room_id删除消息, 逻辑删除
    @staticmethod
    def delete_message_by_room_id(room_id):
        messages = Message.query.filter_by(room_id=room_id, del_flag=1).all()
        for message in messages:
            message.del_flag = 0
        db.session.commit()

    # @staticmethod
    # def get_messages_by_page(page, size, message_param):
    #     messages = Message.query.filter_by(
    #         user_id=message_param.user_id,
    #         room_id=message_param.room_id,
    #         status=1,
    #         role=message_param.role,
    #         model=message_param.model
    #     ).order_by(Message.update_time.desc())
    #     return messages.all()
